import java.util.Stack;

public class LeetCode {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        int i = 0;
        int j = 0;
        Stack<Integer> stack = new Stack<>();
        while(i < pushed.length){
            stack.push(pushed[i]);
            while(!stack.isEmpty()&&popped[j]==stack.peek()){
                stack.pop();
                j++;
            }
            i++;
        }
        if(j== popped.length){
            return true;
        }else{
            return false;
        }
    }
}
